/*
 * AIAttackWeaker.cpp
 *
 *  Created on: 2011-5-5
 *      Author: Administrator
 */

#include "AIAttackWeaker.h"

using namespace Osp::Base::Utility;


namespace Galaxy {
AIAttackWeaker::AIAttackWeaker() {

	__unitsKeeped = (Math::Rand()%8)*5;

}

AIAttackWeaker::~AIAttackWeaker() {
}
void AIAttackWeaker::Play(GalaxyThread* pThread, Player* pMe,
			Osp::Base::Collection::ArrayList* pMyPlanets,
			Osp::Base::Collection::ArrayList* pOtherPlanets,
			Osp::Base::Collection::HashMap* pAccessiblePlanets){
	int myCount = pMyPlanets->GetCount();
	if(myCount == 0){
		return;
	}
	int otherCount = pOtherPlanets->GetCount();
	if(otherCount == 0){
		return;
	}
	bool hasAttack = false;
	for(int i = 0; i < myCount; i++){
		Planet* pPlanet = NULL;
		pPlanet = static_cast<Planet*>(pMyPlanets->GetAt(i));
		if(pPlanet->__soldiersCount >= pPlanet->__maxSoldiers){
			hasAttack = true;
		}else{
			ArrayList* pNearest =static_cast<ArrayList*>(pAccessiblePlanets->GetValue(*pPlanet));
			int size = pNearest->GetCount();
			if(size > 0){
				Planet* pDestination = static_cast<Planet*>(pNearest->GetAt(Math::Rand()%size));
				pThread->Attack(pPlanet, pDestination);
			}
		}
	}
	if(hasAttack){
		return;
	}
	int retry = 0;
	Planet* pSource = NULL;
	while(true){
		int index = Math::Rand()%myCount;
		Planet* pRandPlanet = static_cast<Planet*>(pMyPlanets->GetAt(index));
		if(pRandPlanet->__soldiersCount < this->__unitsKeeped){
			retry++;
			if(retry >= 3){
				break;
			}
			continue;
		}else{
			pSource = pRandPlanet;
			break;
		}
	}
	if(pSource != NULL){
		hasAttack = this->TryAttack(pThread, pSource, pMe, pAccessiblePlanets);
		if(hasAttack){
			return;
		}
	}

	int myPlanetsStrong = 0;
	int myPlanetsWeak = 0;
	for(int j = 0; j < myCount; j++){
		Planet* pPlanet = NULL;
		pPlanet = static_cast<Planet*>(pMyPlanets->GetAt(j));
		if(pPlanet->__soldiersCount < this->__unitsKeeped){
			myPlanetsWeak++;
		}else if(pPlanet->__soldiersCount > pPlanet->__maxSoldiers/2){
			myPlanetsStrong++;
		}
	}
	if(myPlanetsStrong > myPlanetsWeak){
		int destinationPlanetIndex = Math::Rand()%otherCount;
		Planet* pDestPlanet = NULL;
		pDestPlanet = static_cast<Planet*>(pOtherPlanets->GetAt(destinationPlanetIndex));
		for(int k = 0; k < myCount; k++){
			Planet* pSourcePlanet = NULL;
			pSourcePlanet = static_cast<Planet*>(pMyPlanets->GetAt(k));
			pThread->Attack(pSourcePlanet, pDestPlanet);
		}
	}
}

bool AIAttackWeaker::TryAttack(GalaxyThread* pThread, Planet* pSource, Player* pMe, HashMap* pAccessiblePlanets){
	ArrayList* pAccessible = static_cast<ArrayList*>(pAccessiblePlanets->GetValue(*pSource));
	bool hasAttack = false;
	int count = pAccessible->GetCount();
	for(int i = 0; i < count; i++){
		Planet* pPlanet = NULL;
		pPlanet = static_cast<Planet*>(pAccessible->GetAt(i));
		if(pPlanet->__pPlayer == NULL || pPlanet->__pPlayer->__team == pMe->__team){
			int planetDef = 10;
			if(pPlanet->__pPlayer != NULL){
				planetDef = pPlanet->__pPlayer->__defense;
			}
			if(pPlanet->__soldiersCount * planetDef < pSource->__soldiersCount*pMe->__force/2){
				pThread->Attack(pSource, pPlanet);
				if(pSource->__soldiersCount < 5){
					break;
				}
				hasAttack = true;
			}
		}
	}

	return hasAttack;
}
}

